package com.hand.qiandu.Algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 24748  刘瑞锋
 * @date 2019/7/19
 */
public class TwoSum {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for(int i = 0 ;i < nums.length;i++){
            for(int j = i + 1;j < nums.length + 1  ;j++){
                for(int k = j + 1; k < nums.length ; k++)
                if(nums[i] + nums[j] + nums[k] == 0){
                    List<Integer> list1 = new ArrayList<Integer>();
                    list1.add(nums[i]);
                    list1.add(nums[j]);
                    list1.add(nums[k]);
                    if(!isContant(list,list1)){
                        list.add(list1);
                    }

                }
            }
        }
        return list;
    }

    public boolean isContant(List<List<Integer>> list,List<Integer> list1){
        if(list.size() == 0 || list1.isEmpty()){
            return false;
        }
        int disCount = 0;
        for(int i =0 ;i < list.size(); i++){
            int count = 0;
            Object[] array = new Object[list.get(i).size()];
            Object[] array1 = new Object[list.size()];
            array = list.get(i).toArray();
            array1 = list1.toArray();
            Arrays.sort(array);
            Arrays.sort(array1);
            for(int j =0; j < array.length  ;j++){
                if(array[j] != array1[j]){
                    disCount++;
                    break;
                }else{
                    count++;
                }
                if(count == array.length){
                    return true;
                }
            }
        }
        if(disCount == list.size()){
            return false;
        }else{
            return true;
        }


    }
    public static void main(String args[]){
        TwoSum st = new TwoSum();
        int[] nums = {8,-15,-2,-13,8,5,6,-3,-9,3,6,-6,8,14,-9,-8,-9,-6,-14,5,-7,3,-10,-14,-12,-11,12,-15,-1,12,8,-8,12,13,-13,-3,-5,0,10,2,-11,-7,3,4,-8,9,3,-10,11,5,10,11,-7,7,12,-12,3,1,11,9,-9,-4,9,-12,-6,11,-7,4,-4,-12,13,-8,-12,2,3,-13,-12,-8,14,14,12,9,10,12,-6,-1,8,4,8,4,-1,14,-15,-7,9,-14,11,9,5,14};
        List<List<Integer>> list = st.threeSum(nums);
        for(int i = 0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
}
